#include "myScreen.h"

MyScreen::MyScreen()
{
}

MyScreen::~MyScreen()
{
}

void MyScreen::Render()
{
	this->pMyCamera0->SetPosition(this->pMySprite->GetPosition() - Vector3(this->ScreenWidth() / 2.0f, this->ScreenHeight() / 2.0f));
	this->pEngine->GetGraphics()->DrawCamera(this->pMyCamera0);
	this->DrawWorld();

	this->pEngine->GetGraphics()->DrawCamera(this->pMyCamera1);
	this->DrawWorld();

	// After rendering from a camera we must reset the proojetion and modelview
	this->pEngine->GetGraphics()->Setup2DProjection();
	this->pEngine->GetGraphics()->SetModelViewMatrix(Matrix::Identity());
	this->pEngine->GetGraphics()->SetViewport(0, 0, this->ScreenWidth(), this->ScreenHeight());
	

	float fTextX = (this->ScreenWidth() / 2.0f) - (this->pEngine->GetGraphics()->TextWidth(this->sText, this->pMyFont) / 2.0f);
	float fTextY = this->ScreenHeight() - 100.0f;
	this->pEngine->GetGraphics()->SetGlobalColor(Color(0, 0, 0));
	this->pEngine->GetGraphics()->DrawText(this->sText, fTextX + 3, fTextY + 3, this->pMyFont);
	this->pEngine->GetGraphics()->SetGlobalColor(Color(255, 255, 255));
	this->pEngine->GetGraphics()->DrawText(this->sText, fTextX, fTextY, this->pMyFont);
}

void MyScreen::Update(float fDelta)
{
	this->pMySprite->Update(fDelta);

	if (this->pEngine->GetInput()->IsKeyDown(DelGE_Key_Left))
		this->pMySprite->SetPosition(this->pMySprite->GetPosition() + (Vector3(-100.0f * fDelta, 0)));

	if (this->pEngine->GetInput()->IsKeyDown(DelGE_Key_Right))
		this->pMySprite->SetPosition(this->pMySprite->GetPosition() + (Vector3(100.0f * fDelta, 0)));

	if (this->pEngine->GetInput()->IsKeyDown(DelGE_Key_Up))
		this->pMySprite->SetPosition(this->pMySprite->GetPosition() + (Vector3(0, -100.0f * fDelta)));

	if (this->pEngine->GetInput()->IsKeyDown(DelGE_Key_Down))
		this->pMySprite->SetPosition(this->pMySprite->GetPosition() + (Vector3(0, 100.0f * fDelta)));
}

void MyScreen::Load()
{
	// Setup the cameras by passing in the camera viewport
	this->pMyCamera0 = new Camera(0, 0, this->ScreenWidth(), this->ScreenHeight());
	this->pMyCamera0->SetOrthographicProjection(0, (float)this->ScreenWidth(), (float)this->ScreenHeight(), 0, -1, 1);
	this->pMyCamera0->SetBackgroundColor(Color(255, 189, 38));

	this->pMyCamera1 = new Camera(10, 10, 200, 200);
	this->pMyCamera1->SetOrthographicProjection(0, (float)this->ScreenWidth(), (float)this->ScreenHeight(), 0, -1, 1);
	this->pMyCamera1->SetBackgroundColor(Color(0, 0, 0));

	// Load the textures we will need
	this->pMyTexture = this->pEngine->GetGraphics()->LoadTexture("media/animation.jpg");
	this->pMyBackground = this->pEngine->GetGraphics()->LoadTexture("media/bkgd11.png");

	// This is our "actor sprite"
	this->pMySprite = new Sprite(this->pMyTexture, 96, 96);
	this->pMySprite->SetHandlePosition(0.5f, 0.5f);
	this->pMySprite->SetPosition(this->ScreenWidth() / 2.0f, this->ScreenHeight() / 2.0f);

	this->pMyFont = this->pEngine->GetGraphics()->LoadFont("media/8bitlim.ttf", 24);

	this->sText = "<Use the arrow keys to move the actor>";
}

void MyScreen::Unload()
{
	this->pEngine->GetGraphics()->UnloadTexture(this->pMyTexture);
	this->pEngine->GetGraphics()->UnloadTexture(this->pMyBackground);
	this->pEngine->GetGraphics()->UnloadFont(this->pMyFont);

	delete this->pMySprite;

	delete this->pMyCamera0;
	delete this->pMyCamera1;
}

void MyScreen::DrawWorld()
{
	this->pEngine->GetGraphics()->SetGlobalColor(Color(255, 255, 255));
	this->pEngine->GetGraphics()->DrawTexture(this->pMyBackground, 0, 0, (float)this->ScreenWidth(), (float)this->ScreenHeight());
	this->pEngine->GetGraphics()->DrawSprite(this->pMySprite);
}